<?php

namespace app\api\controller\common;

use app\common\controller\Api;
use think\facade\Db;

/**
 * 展示数据的接口
 * 将全部的时间日期改成自然获取
 */
class Data extends Api
{
	protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 五个区的专利数量
    public function patent_list()
    {
    	// 本月
    	$month = date('Y-m',time());
    	// $month = input('month') ?? '';
    	// 五个区
    	$address = Db::name('address')
    				->where('id','>',0)
    				->order('weigh desc')
    				->select()->toArray();
    	// 数据数组  --柱状图
    	$count = [];
    	$auth = [];
    	$news = [];
    	$apper = [];
    	$arr = [];

    	// 获取最新时间的专利数量
    	if (!empty($address)) {
    		foreach ($address as $k => $v) {
    			$parent = Db::name('data_patent')
    					->where('date',$month)
    					->where('address_id',$v['id'])
    					->find();
    			$count[$k] = $parent['count'];  // 专利总数
    			$auth[$k] = $parent['auth']; // 发明授权
    			$news[$k] = $parent['news']; // 新型授权
    			$apper[$k] = $parent['apper']; // 外观授权

    			// 柱状图的数组
    			$auth_temp[$k] = [
    				'value'=>$parent['auth'],
    				'name'=>$v['name'],
    			];
    			$news_temp[$k] = [
    				'value'=>$parent['news'],
    				'name'=>$v['name'],
    			];
    			$apper_temp[$k] = [
    				'value'=>$parent['apper'],
    				'name'=>$v['name'],
    			];
    		}
    	}

    	$arr['count'] = array_sum($count);
    	$arr['auth'] = array_sum($auth);
    	$arr['news'] = array_sum($news);
    	$arr['apper'] = array_sum($apper);

        // 添加展示五个区的专利数量
        foreach ($address as $k => $v) {
            $temp = Db::name('data_patent')
                        ->where('date',$month)
                        ->where('address_id',$v['id'])
                        ->find();
            $list[$k]['name'] = $v['name'];
            $list[$k]['value']['auth'] = $temp['auth'];
            $list[$k]['value']['news'] = $temp['news'];
            $list[$k]['value']['apper'] = $temp['apper'];
            $list[$k]['value']['count'] = $temp['count'];
        }
    	
		$data = [
			'columnar'=>[
				'count'=>$count,
				'auth'=>$auth,
				'news'=>$news,
				'apper'=>$apper,
			],
			'cake'=>[
				'auth'=>$auth_temp,
				'news'=>$news_temp,
				'apper'=>$apper_temp,
			],
			'arr'=>$arr,
            'list'=>$list,
		];
		$this->success('success',$data);
    }

    // 高新区3+3+x产业专利情况
    public function industry_list()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';

        // 获取传入月份的最后一天时间
        $BeginDate = date('Y-m-01',strtotime($month));
        $month_start =  date('Y-m-d', strtotime("$BeginDate +0 month -1 day"));
        $time_temp = explode('-',$month_start);
        $time = '截止'.$time_temp[0].'年'.$time_temp[1].'月'.$time_temp[2].'日';

        $arr = [];

        $industry = Db::name('industry')->where('id','>',0)->select()->toArray();
        // 获取每个产业的总数
        foreach ($industry as $k => $v) {
            $temp = Db::name('data_industry')
                        ->field('id,industry_id,date,num')
                        ->where('date',$month)
                        ->where('industry_id',$v['id'])
                        ->select()->toArray();
            $num_arr = array_column($temp, 'num');
            $value = array_sum($num_arr);
            $arr[$k] = [
                'name'=>$v['name'],
                'value'=>$value,
            ];
        }
        // 按照数值大小排序
        $edit = array_column($arr,'value');
        array_multisort($edit,SORT_DESC,$arr);
        // 计算总数
        $count = array_sum($edit);
        $t1 = round($arr[0]['value']/$count*100,1);
        $t2 = round($arr[1]['value']/$count*100,1);
        $n1 = $arr[0]['name'];
        $n2 = $arr[1]['name'];
        $n3 = $arr[2]['name'];
        $n4 = $arr[3]['name'];
        $n5 = $arr[4]['name'];
        $n6 = $arr[5]['name'];
        $n7 = $arr[6]['name'];

        $p1 = $arr[0]['value'];
        $p2 = $arr[1]['value'];


        // 获取排名前面的产业
        $words = '佛山国家高新区重点产业专利总量共'.$count.'件，其中'.$n1.$p1.'件，占比'.$t1.'%;其次是'.$n2.$p2.'件，占比'.$t2.'%;其后分别是'.$n3.'、'.$n4.'、'.$n5.'、'.$n6.'、'.$n7.'。';
        $data = [
            'cake'=>$arr,
            'words'=>$words,
            'time'=>$time,
        ];
        $this->success('success',$data);
    }

    // 细分产业情况首页
    // 展示各个产业的总量
    // 这个月和前一个月的增量
    public function fineall_list()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m', strtotime(date('Y-m-01') . " - 1 month"));

        // $month = input('month') ?? '';
        // $last_month = input('last_month') ?? '';

        $arr = [];
        $arr_list = [];//饼状

        $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();

        // 五大区
        $address = Db::name('address')->where('id','>',0)->order('weigh desc')->select()->toArray();
        $address_arr = array_column($address,'name','id');
        foreach ($industry as $k => $v) {
            $list = Db::name('data_industry')
                    ->where('date',$month)
                    ->where('industry_id',$v['id'])
                    ->select()->toArray();
            $temp = array_column($list,'num');
            $arr[$k]['industry_id'] = $v['id'];
            $arr[$k]['name'] = $v['name'];
            $num = array_sum($temp);
            $arr[$k]['num'] = $num;  //本月这个产业的总数

            $list_last = Db::name('data_industry')
                    ->where('date',$last_month)
                    ->where('industry_id',$v['id'])
                    ->select()->toArray();
            $temp_last = array_column($list_last,'num');
            $num_last = array_sum($temp_last);
            $arr[$k]['add'] = $num - $num_last; //本月减去上个月

            // 获取五个区的各自的数量  本月的
            foreach ($list as $key => $value) {
                $arr_list[$k][$key] = [
                    'name'=>$address_arr[$value['address_id']],
                    'value'=>$value['num'],
                ];
            }
        }
        $data = [
            'arr'=>$arr,
            'cake'=>$arr_list,
        ];
        $this->success('success',$data);
    }

    // 细分产业专利情况
    public function fine_list()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m', strtotime(date('Y-m-01') . " - 1 month"));
        $last_year_month = date('Y-m',strtotime('-1 year'));
        // $month = input('month') ?? '';
        // $last_month = input('last_month') ?? '';
        // $last_year_month = input('last_year_month') ?? '';  // 去年本月
        
        $industry_id = input('industry_id') ?? '';//默认不传为全部

        // 五大区
        $address = Db::name('address')->where('id','>',0)->order('weigh desc')->select()->toArray();
        $address_arr = array_column($address,'name','id');
        $address_list = array_column($address,'name');

        $columnar = [];

        if (empty($industry_id)) {  //如果不传为全部
            $arr = [];
            $count = count($address);
            $arr_num = [
                'id'=>$count,
                'name'=>'整体',
                'add'=>0,
                'count'=>0,
                'open'=>0,
                'num'=>0,
                'auth'=>0,
                'news'=>0,
                'apper'=>0,
                'lose'=>0,
            ]; //用于相加后的
            
            foreach ($address as $k => $v) {
                $list = Db::name('data_industry')
                        ->where('date',$month)
                        ->where('address_id',$v['id'])
                        ->select()->toArray();
                $list_one = Db::name('data_industry')
                        ->where('date',$last_month)
                        ->where('address_id',$v['id'])
                        ->select()->toArray();
                $list_two = Db::name('data_industry')
                        ->where('date',$last_year_month)
                        ->where('address_id',$v['id'])
                        ->select()->toArray();
                $columnar['last_month'][$k] = array_sum(array_column($list_one,'count'));
                $columnar['last_year_month'][$k] = array_sum(array_column($list_two,'count'));

                $arr[$k]['id'] = $k + 1;
                $arr[$k]['name'] = $v['name'];

                $arr[$k]['count'] = array_sum(array_column($list,'count'));
                // 添加柱状图数据
                $columnar['this_month'][$k] = $arr[$k]['count'];
                $arr[$k]['open'] = array_sum(array_column($list,'open'));
                $arr[$k]['num'] = array_sum(array_column($list,'num'));
                $arr[$k]['auth'] = array_sum(array_column($list,'auth'));
                $arr[$k]['news'] = array_sum(array_column($list,'news'));
                $arr[$k]['apper'] = array_sum(array_column($list,'apper'));
                $arr[$k]['lose'] = array_sum(array_column($list,'lose'));

                // 当前月的新增
                $temp = Db::name('data_industry')
                        ->where('date',$last_month)
                        ->where('address_id',$v['id'])
                        ->select()->toArray();
                $arr[$k]['add'] = $arr[$k]['count'] - array_sum(array_column($temp,'count'));
            }
            $arr_num = [
                'id'=>$count,
                'name'=>'整体',
                'add'=>array_sum(array_column($arr,'add')),
                'count'=>array_sum(array_column($arr,'count')),
                'open'=>array_sum(array_column($arr,'open')),
                'num'=>array_sum(array_column($arr,'num')),
                'auth'=>array_sum(array_column($arr,'auth')),
                'news'=>array_sum(array_column($arr,'news')),
                'apper'=>array_sum(array_column($arr,'apper')),
                'lose'=>array_sum(array_column($arr,'lose')),
            ];
            $arr[$count] = $arr_num;
            $data = [
                'list'=>$arr,
                'address'=>$address_list,
                'columnar'=>$columnar,
            ];
            $this->success('success',$data);
        }

        

        $list = Db::name('data_industry')
                ->where('date',$month)
                ->where('industry_id',$industry_id)
                ->select()->toArray();
        $list_one = Db::name('data_industry')
                ->where('date',$last_month)
                ->where('industry_id',$industry_id)
                ->select()->toArray();  //上个月
        $list_two = Db::name('data_industry')
                ->where('date',$last_year_month)
                ->where('industry_id',$industry_id)
                ->select()->toArray();  // 去年本月
        foreach ($list_one as $k => $v) {
            $columnar['last_month'][$k] = $v['count'];
        }
        foreach ($list_two as $k => $v) {
            $columnar['last_year_month'][$k] = $v['count'];
        }
        $count = count($list);
        $arr = [];
        $arr_num = [
            'id'=>$count,
            'name'=>'整体',
            'add'=>0,
            'count'=>0,
            'open'=>0,
            'num'=>0,
            'auth'=>0,
            'news'=>0,
            'apper'=>0,
            'lose'=>0,
        ]; //用于相加后的
        foreach ($list as $k => $v) {
            $arr[$k]['id'] = $k+1;
            $arr[$k]['name'] = $address_arr[$v['address_id']];
            // 获取当月和上个月的增量
            $temp = Db::name('data_industry')
                    ->where('date',$last_month)
                    ->where('industry_id',$industry_id)
                    ->where('address_id',$v['address_id'])
                    ->value('count');
            $arr[$k]['add'] = $v['count'] - $temp;

            $arr[$k]['count'] = $v['count'];
            // 添加记录本月总量
            $columnar['this_month'][$k] = $v['count'];
            $arr[$k]['open'] = $v['open'];
            $arr[$k]['num'] = $v['num'];
            $arr[$k]['auth'] = $v['auth'];
            $arr[$k]['news'] = $v['news'];
            $arr[$k]['apper'] = $v['apper'];
            $arr[$k]['lose'] = $v['lose'];

            $arr_num['count'] = $arr_num['count'] + $v['count'];
            $arr_num['open'] = $arr_num['open'] + $v['open'];
            $arr_num['num'] = $arr_num['num'] + $v['num'];
            $arr_num['auth'] = $arr_num['auth'] + $v['auth'];
            $arr_num['news'] = $arr_num['news'] + $v['news'];
            $arr_num['apper'] = $arr_num['apper'] + $v['apper'];
            $arr_num['lose'] = $arr_num['lose'] + $v['lose'];
        }
        $arr[$count] = $arr_num;
        
        $data = [
            'list'=>$arr,
            'address'=>$address_list,
            'columnar'=>$columnar,
        ];
        $this->success('success',$data);
    }

    //===========================================================
    // 园区企业专利排名  总量排名  
    public function company_list()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m', strtotime(date('Y-m-01') . " - 1 month"));
        // $month = input('month') ?? '';
        $limit = input('limit') ?? 10;
        $address_id = input('address_id') ?? '';
        if (empty($limit)) {
            $limit = 10;
        }
        // $last_month = '2022-11';
        
        $where = [];
        if (!empty($address_id)) {
            $where[] = ['address_id','=',$address_id];
        }

        $list = Db::name('data_company')
                ->where('date',$month)
                ->where($where)
                ->order('count desc')
                ->limit($limit)
                ->select()->toArray();
        foreach ($list as $k => $v) {
            $list[$k]['ranking'] = $k + 1;
            $temp = Db::name('data_company')
                    ->where('date',$last_month)
                    ->where('company',$v['company'])
                    ->where('address_id',$v['address_id'])
                    ->value('count');
            $list[$k]['add'] = $v['count'] - $temp;
            $list[$k]['address'] = Db::name('address')->where('id',$v['address_id'])->value('name');
        }
        $this->success('success',$list);
    }
    // 园区企业专利排名    新增排名
    public function company_add_list()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';
        $limit = input('limit') ?? 10;
        $address_id = input('address_id') ?? '';
        if (empty($limit)) {
            $limit = 10;
        }
        $last_month = '2022-11';
        
        $where = [];
        if (!empty($address_id)) {
            $where[] = ['address_id','=',$address_id];
        }

        $list = Db::name('data_company_add')
                ->where('date',$month)
                ->where($where)
                ->order('count desc')
                ->limit($limit)
                ->select()->toArray();
        foreach ($list as $k => $v) {
            $list[$k]['ranking'] = $k + 1;
            $list[$k]['address'] = Db::name('address')->where('id',$v['address_id'])->value('name');
        }
        $this->success('success',$list);
    }

    //==========================================================
    // 顶尖团队专利情况
    public function team_list()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';
        // 两种逻辑进行区分 
        $type = input('type') ?? 1;
        if($type==2) {
            $table_name = 'data_base_team_add';
        }else{
            $table_name = 'data_team';
        }

        // 获取传入月份的最后一天时间
        $BeginDate = date('Y-m-01',strtotime($month));
        $month_start =  date('Y-m-d', strtotime("$BeginDate +0 month -1 day"));
        $time_temp = explode('-',$month_start);
        $time = '截止'.$time_temp[0].'年'.$time_temp[1].'月'.$time_temp[2].'日';

        $arr = [];

        $industry = Db::name('industry')->where('id','>',0)->select()->toArray();
        // 获取每个产业的总数
        foreach ($industry as $k => $v) {
            $temp = Db::name($table_name)
                        ->field('id,industry_id,date,num')
                        ->where('date',$month)
                        ->where('industry_id',$v['id'])
                        ->select()->toArray();
            $num_arr = array_column($temp, 'num');
            $value = array_sum($num_arr);
            $arr[$k] = [
                'name'=>$v['name'],
                'value'=>$value,
            ];
        }
        // 按照数值大小排序
        $edit = array_column($arr,'value');
        array_multisort($edit,SORT_DESC,$arr);
        // 计算总数
        $count = array_sum($edit);
        $t1 = round($arr[0]['value']/$count*100,1);
        $t2 = round($arr[1]['value']/$count*100,1);
        $n1 = $arr[0]['name'];
        $n2 = $arr[1]['name'];
        $n3 = $arr[2]['name'];
        $n4 = $arr[3]['name'];
        $n5 = $arr[4]['name'];
        $n6 = $arr[5]['name'];
        $n7 = $arr[6]['name'];

        $p1 = $arr[0]['value'];
        $p2 = $arr[1]['value'];


        // 获取排名前面的产业
        $words = '重点产业高校团队专利申请总量共'.$count.'件，其中'.$n1.$p1.'件，占比'.$t1.'%;其次是'.$n2.$p2.'件，占比'.$t2.'%;其后分别是'.$n3.'、'.$n4.'、'.$n5.'、'.$n6.'、'.$n7.'。';
        $data = [
            'cake'=>$arr,
            'words'=>$words,
            'time'=>$time,
        ];
        $this->success('success',$data);
    }

    // 顶尖团队产业专利饼图
    public function team_cake_list()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m', strtotime(date('Y-m-01') . " - 1 month"));
        // $month = input('month') ?? '';
        // $last_month = input('last_month') ?? '';
        // 两种逻辑进行区分 
        $type = input('type') ?? 1;
        if($type==2) {
            $table_name = 'data_base_team_add';
        }else{
            $table_name = 'data_team';
        }

        $arr = [];
        $arr_list = [];//饼状

        $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();

        // 五大区
        // $address = Db::name('address')->where('id','>',0)->order('weigh desc')->select()->toArray();
        // $address_arr = array_column($address,'name','id');
        foreach ($industry as $k => $v) {
            $list = Db::name($table_name)
                    ->where('date',$month)
                    ->where('industry_id',$v['id'])
                    ->select()->toArray();
            $temp = array_column($list,'count');
            $arr[$k]['industry_id'] = $v['id'];
            $arr[$k]['name'] = $v['name'];
            $num = array_sum($temp);
            $arr[$k]['num'] = $num;  //本月这个产业的总数

            $list_last = Db::name($table_name)
                    ->where('date',$last_month)
                    ->where('industry_id',$v['id'])
                    ->select()->toArray();
            $temp_last = array_column($list_last,'count');
            $num_last = array_sum($temp_last);
            $arr[$k]['add'] = $num - $num_last; //本月减去上个月

            // 获取五个区的各自的数量  本月的
            // foreach ($list as $key => $value) {
            //     $arr_list[$k][$key] = [
            //         'name'=>$address_arr[$value['address_id']],
            //         'value'=>$value['num'],
            //     ];
            // }
        }
        $data = [
            'arr'=>$arr,
            // 'cake'=>$arr_list,
        ];
        $this->success('success',$data);
    }

    // 顶尖团队的本月、上月、去年本月
    public function team_columnar()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m', strtotime(date('Y-m-01') . " - 1 month"));
        $last_year_month = date('Y-m',strtotime('-1 year'));
        // $month = input('month') ?? '';
        // $last_month = input('last_month') ?? '';
        // $last_year_month = input('last_year_month') ?? '';  // 去年本月
        // 两种逻辑进行区分 
        $type = input('type') ?? 1;
        if($type==2) {
            $table_name = 'data_base_team_add';
        }else{
            $table_name = 'data_team';
        }
        
        $this_month_arr = [];
        $last_month_arr = [];
        $last_year_month_arr = [];
        $columnar = [];

        // 七大产业
        $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();
        $industry_list = array_column($industry,'name');

        foreach ($industry as $k => $v) {
            $count1 = Db::name($table_name)->where('industry_id',$v['id'])->where('date',$month)->value('count');
            $count2 = Db::name($table_name)->where('industry_id',$v['id'])->where('date',$last_month)->value('count');
            $count3 = Db::name($table_name)->where('industry_id',$v['id'])->where('date',$last_year_month)->value('count');
            // var_dump($count3);exit;
            $this_month_arr[$k] = !empty($count1)?$count1:0;
            $last_month_arr[$k] = !empty($count2)?$count2:0;
            $last_year_month_arr[$k] = !empty($count3)?$count3:0;
        }
        $columnar = [
            'this_month'=>$this_month_arr,
            'last_month'=>$last_month_arr,
            'last_year_month'=>$last_year_month_arr,
        ];


        $data = [
            'industry'=>$industry_list,
            'columnar'=>$columnar,
        ];
        $this->success('success',$data);

    }


    // 新的逻辑团队专利
    // 增加排名
    // 默认为全产业的排名，可以选择某个产业的排名
    // 并且返回可以筛选的团队和所属高校
    public function rank_team()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = '2023-02';
        $industry_id = input('industry_id') ?? 0;

        // 加筛选
        // 需要二级联动
        $team_name = input('team_name') ?? '';
        $aspo = input('aspo') ?? '';
        $keyword = input('keyword') ?? '';

        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;

        $where = [];
        if (!empty($team_name)) {
            $where[] = ['team_name','=',$team_name];
        }
        if (!empty($aspo)) {
            $where[] = ['aspo','=',$aspo];
        }
        if (!empty($keyword)) {
            $where[] = ['aspo|team_name','like','%'.$keyword.'%'];
        }

        if (empty($industry_id)) {
            // 需要将相同的团队进行合并
            $list = Db::name('data_base_team')
                    ->field('id,team_name,aspo,sum(num) as num,sum(auth) as auth,sum(news) as news,sum(apper) as apper,industry_id')
                    ->where('date',$month)
                    ->where($where)
                    ->group('team_name,aspo,industry_id')
                    ->order('num desc')
                    ->page($page,$page_size)
                    ->select()->toArray();
            $count = Db::name('data_base_team')
                    ->where('date',$month)
                    ->where($where)
                    ->group('team_name,aspo')
                    ->count();
            foreach ($list as $k => $v) {
                $list[$k]['rank'] = $k + 1;
            }
            // 筛选条件
            $aspo = [];
            $team_name = [];
            $aspo_t = array_unique(array_column(Db::name('data_base_team')
                    ->field('aspo')
                    ->where('date',$month)
                    ->order('num desc')
                    ->select()->toArray(),'aspo'));
            $aspo_t = array_values($aspo_t);
            foreach ($aspo_t as $k => $v) {
                $aspo[$k]['label'] = $v;
            }
            $team_name_t = array_unique(array_column(Db::name('data_base_team')
                    ->field('team_name')
                    ->where('date',$month)
                    ->order('num desc')
                    ->select()->toArray(),'team_name'));
            $team_name_t = array_values($team_name_t);
            foreach ($team_name_t as $k => $v) {
                $team_name[$k]['label'] = $v;
            }
            $data = [
                'page'=>$page,
                'page_size'=>$page_size,
                'count'=>$count,
                'list'=>$list,
                'aspo'=>$aspo,
                'team_name'=>$team_name,
            ];
            $this->success('success',$data);
        }else{
            $list = Db::name('data_base_team')
                    ->field('id,team_name,aspo,sum(num) as num,sum(auth) as auth,sum(news) as news,sum(apper) as apper,industry_id')
                    ->where('date',$month)
                    ->where($where)
                    ->where('industry_id',$industry_id)
                    ->group('team_name,aspo,industry_id')
                    ->page($page,$page_size)
                    ->order('num desc')
                    ->select()->toArray();
            $count = Db::name('data_base_team')
                    ->where('date',$month)
                    ->where($where)
                    ->where('industry_id',$industry_id)
                    ->group('team_name,aspo')
                    ->count();
            foreach ($list as $k => $v) {
                $list[$k]['rank'] = $k + 1;
            }
            // 筛选条件
            $aspo = [];
            $team_name = [];
            $aspo_t = array_unique(array_column(Db::name('data_base_team')
                    ->field('aspo')
                    ->where('date',$month)
                    ->where('industry_id',$industry_id)
                    ->order('num desc')
                    ->select()->toArray(),'aspo'));
            $aspo_t = array_values($aspo_t);
            foreach ($aspo_t as $k => $v) {
                $aspo[$k]['label'] = $v;
            }
            $team_name_t = array_unique(array_column(Db::name('data_base_team')
                    ->field('team_name')
                    ->where('date',$month)
                    ->where('industry_id',$industry_id)
                    ->order('num desc')
                    ->select()->toArray(),'team_name'));
            $team_name_t = array_values($team_name_t);
            foreach ($team_name_t as $k => $v) {
                $team_name[$k]['label'] = $v;
            }
            $data = [
                'page'=>$page,
                'page_size'=>$page_size,
                'count'=>$count,
                'list'=>$list,
                'aspo'=>$aspo,
                'team_name'=>$team_name,
            ];
            $this->success('success',$data);
        }
    }

    // 二级联动筛选
    public function go_seach()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = '2023-02';
        $industry_id = input('industry_id') ?? 0;

        // 加筛选
        // 需要二级联动
        $team_name = input('team_name') ?? '';
        $aspo = input('aspo') ?? '';
        
        if (empty($industry_id)) {
            
            // 判断选择了所属高校 还是团队
            // 选择所属高校后 只展示对应团队
            if (!empty($team_name) && empty($aspo)) {
                $aspo_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('aspo')
                        ->where('team_name',$team_name)
                        ->where('date',$month)
                        ->order('num desc')
                        ->select()->toArray(),'aspo'));
                $aspo_t = array_values($aspo_t);
                $aspo = [];
                foreach ($aspo_t as $k => $v) {
                    $aspo[$k]['label'] = $v;
                }
                $data = [
                    'aspo'=>$aspo,
                    'team_name'=>[['label'=>$team_name]],
                ];
                $this->success('success',$data);
            }

            // 选择团队后 只展示所属高校
            if (empty($team_name) && !empty($aspo)) {
                $team_name_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('team_name')
                        ->where('aspo',$aspo)
                        ->where('date',$month)
                        ->order('num desc')
                        ->select()->toArray(),'team_name'));
                $team_name_t = array_values($team_name_t);
                $team_name = [];
                foreach ($team_name_t as $k => $v) {
                    $team_name[$k]['label'] = $v;
                }
                $data = [
                    'aspo'=>[['label'=>$aspo]],
                    'team_name'=>$team_name,
                ];
                $this->success('success',$data);
            }


            // 这是初始状态下
            if (empty($team_name) && empty($aspo)) {
                $aspo_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('aspo')
                        ->where('date',$month)
                        ->order('num desc')
                        ->select()->toArray(),'aspo'));
                $aspo_t = array_values($aspo_t);
                $aspo = [];
                $team_name = [];
                foreach ($aspo_t as $k => $v) {
                    $aspo[$k]['label'] = $v;
                }
                $team_name_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('team_name')
                        ->where('date',$month)
                        ->order('num desc')
                        ->select()->toArray(),'team_name'));
                $team_name_t = array_values($team_name_t);
                foreach ($team_name_t as $k => $v) {
                    $team_name[$k]['label'] = $v;
                }
                $data = [
                    'aspo'=>$aspo,
                    'team_name'=>$team_name,
                ];
                $this->success('success',$data);
            }
            $data = [
                'aspo'=>[['label'=>$aspo]],
                'team_name'=>[['label'=>$team_name]],
            ];
            $this->success('success',$data);
            
        }else{
            
            // 判断选择了所属高校 还是团队
            // 选择所属高校后 只展示对应团队
            if (!empty($team_name) && empty($aspo)) {
                $aspo_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('aspo')
                        ->where('team_name',$team_name)
                        ->where('industry_id',$industry_id)
                        ->where('date',$month)
                        ->order('num desc')
                        ->select()->toArray(),'aspo'));
                $aspo_t = array_values($aspo_t);
                $aspo = [];
                foreach ($aspo_t as $k => $v) {
                    $aspo[$k]['label'] = $v;
                }
                $data = [
                    'aspo'=>$aspo,
                    'team_name'=>[['label'=>$team_name]],
                ];
                $this->success('success',$data);
            }

            // 选择团队后 只展示所属高校
            if (empty($team_name) && !empty($aspo)) {
                $team_name_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('team_name')
                        ->where('aspo',$aspo)
                        ->where('industry_id',$industry_id)
                        ->where('date',$month)
                        ->order('num desc')
                        ->select()->toArray(),'team_name'));
                $team_name_t = array_values($team_name_t);
                $team_name = [];
                foreach ($team_name_t as $k => $v) {
                    $team_name[$k]['label'] = $v;
                }
                $data = [
                    'aspo'=>[['label'=>$aspo]],
                    'team_name'=>$team_name,
                ];
                $this->success('success',$data);
            }


            // 这是初始状态下
            if (empty($team_name) && empty($aspo)) {
                $aspo_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('aspo')
                        ->where('date',$month)
                        ->where('industry_id',$industry_id)
                        ->order('num desc')
                        ->select()->toArray(),'aspo'));
                $aspo_t = array_values($aspo_t);
                $team_name = [];
                $aspo = [];
                foreach ($aspo_t as $k => $v) {
                    $aspo[$k]['label'] = $v;
                }
                $team_name_t = array_unique(array_column(Db::name('data_base_team')
                        ->field('team_name')
                        ->where('date',$month)
                        ->where('industry_id',$industry_id)
                        ->order('num desc')
                        ->select()->toArray(),'team_name'));
                $team_name_t = array_values($team_name_t);
                foreach ($team_name_t as $k => $v) {
                    $team_name[$k]['label'] = $v;
                }
                $data = [
                    'aspo'=>$aspo,
                    'team_name'=>$team_name,
                ];
                $this->success('success',$data);
            }
            $data = [
                'aspo'=>[['label'=>$aspo]],
                'team_name'=>[['label'=>$team_name]],
            ];
            $this->success('success',$data);
        }
    }

    // 加一个需要展示数量专利的接口
    // type 默认是专利总数量的接口
    public function go_patent()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = '2023-02';
        $industry_id = input('industry_id') ?? 0;

        // 必须的参数
        $team_name = input('team_name') ?? '';
        $aspo = input('aspo') ?? '';
        $type = input('type') ?? 0;

        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        
        if (empty($team_name) || empty($aspo)) {
            $this->error('error','参数不全');
        }

        $where = [];
        if (!empty($industry_id)) {
            $where[] = ['industry_id','=',$industry_id];
        }
        if (!empty($type)) {  // 默认总数 1发明 2新型 3外观
            if ($type==1) {
                $where[] = ['pdt','=','发明'];
            }
            if ($type==2) {
                $where[] = ['pdt','=','实用新型'];
            }
            if ($type==3) {
                $where[] = ['pdt','=','外观设计'];
            }
        }

        // 在data_base表中查找对应的专利
        $list = Db::name('data_base')
                ->where($where)
                ->where('inc_one','=',$aspo)
                ->where('aspo','like','%'.$team_name.'%')
                ->where('date',$month)
                ->page($page,$page_size)
                ->select()->toArray();
        $count = Db::name('data_base')
                ->where($where)
                ->where('inc_one','=',$aspo)
                ->where('aspo','like','%'.$team_name.'%')
                ->where('date',$month)
                ->count();
        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);
    }


    //=========================================================
    //各园区重点产业专利分布及新增专利
    // 本月减去上个月
    // 修改 主要产业为当月数量最多的
    public function important_industry()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m', strtotime(date('Y-m-01') . " - 1 month"));

        // 加入日期
        $BeginDate = date('Y-m-01',strtotime($month));
        $month_start =  date('Y.m.d', strtotime("$BeginDate +0 month -1 day"));
        $time1 = date('Y.m.d', strtotime(date('Y-m-01') . " - 1 month"));
        $time = $time1.'-'.$month_start;

        // $month = input('month') ?? '';
        // $last_month = input('last_month') ?? '';
        $temp = Db::name('address')->where('id','>',0)->order('weigh desc')->select()->toArray();
        $address = array_column($temp,'name');
        $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();
        $industry_arr = array_column($industry,'name','id');
        $industry_list = array_column($industry,'name');
        $columnar = [];
        $arr = [];
        foreach ($temp as $k => $v) {
            foreach ($industry as $k1 => $v1) {
                $list = Db::name('data_industry')
                        ->where('date',$month)
                        ->where('address_id',$v['id'])
                        ->where('industry_id',$v1['id'])
                        ->find();
                $columnar[$k1][$k] = $list['count'];
            }
            $t1 = Db::name('data_industry')->where('date',$month)->where('address_id',$v['id'])->select()->toArray();
            $t2 = Db::name('data_industry')->where('date',$last_month)->where('address_id',$v['id'])->select()->toArray();
            $num1 = array_sum(array_column($t1,'count'));
            $num2 = array_sum(array_column($t2,'count'));
            $arr[$k]['address'] = $v['name'];
            $arr[$k]['num'] = $num1 - $num2;
            // var_dump($arr[$k]['num']);
            // var_dump($num1);
            // var_dump($num2);exit;
            // 获取数量最多的为该园区主要产业
            $import = Db::name('data_industry')->where('address_id',$v['id'])->where('date',$month)->order('count desc')->limit(1)->select();
            $industry_id = $import[0]['industry_id'];
            $t3 = Db::name('data_industry')->where('date',$month)->where('industry_id',$industry_id)->where('address_id',$v['id'])->select()->toArray();
            $t4 = Db::name('data_industry')->where('date',$last_month)->where('industry_id',$industry_id)->where('address_id',$v['id'])->select()->toArray();
            $num3 = array_sum(array_column($t3,'count'));
            $num4 = array_sum(array_column($t4,'count'));
            $arr[$k]['industry'] = $industry_arr[$industry_id];
            $arr[$k]['add'] = $num3 - $num4; 
        }

        $data = [
            'address'=>$address,
            'columnar'=>$columnar,
            'arr'=>$arr,
            'industry'=>$industry_list,
            'time'=>$time,
        ];
        $this->success('success',$data);
    }

    // 直接展示5个区 或者5个园
    public function address()
    {
        $list = Db::name('address')->where('id','>',0)->select()->toArray();
        $industry_arr = Db::name('industry')->where('id','>',0)->select()->toArray();
        $data = [];
        foreach ($list as $k => $v) {
            $address[$k]['id'] = $v['id'];
            $temp = mb_substr($v['name'],0,2);
            $address[$k]['name1'] = $temp.'区';
            $address[$k]['name2'] = $temp.'园';
        }
        foreach ($industry_arr as $key => $value) {
            $industry[$key]['id'] = $value['id'];
            $industry[$key]['name'] = $value['name'];
        }
        $data = [
            'address'=>$address,
            'industry'=>$industry,
        ];
        $this->success('success',$data);
    }

}